<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Java time migration guide | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'migrate-to-java-time.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/migrate-to-java-time.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/migrate-to-java-time.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/migrate-to-java-time.html" rel="nofollow" target="_blank">../en/migrate-to-java-time.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="breaking-changes.html">Breaking changes</a></span>
»
<span class="breadcrumb-link"><a href="breaking-changes-7.0.html">Breaking changes in 7.0</a></span>
»
<span class="breadcrumb-node">Java time migration guide</span>
</div>
<div class="navheader">
<span class="prev">
<a href="breaking-changes-7.0.html">« Breaking changes in 7.0</a>
</span>
<span class="next">
<a href="es-release-notes.html">Release notes »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="migrate-to-java-time"></a>Java time migration guide<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/migration/migrate_7_0/migrate_to_java_time.asciidoc">edit</a>
</h2>
</div></div></div>
<p>With 7.0, Elasticsearch switched from joda time to java time for date-related parsing,
formatting, and calculations. This guide is designed to help you determine
if your cluster is impacted and, if so, prepare for the upgrade.</p>
<h3>
<a id="java-time-convert-date-formats"></a>Convert date formats<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/migration/migrate_7_0/migrate_to_java_time.asciidoc">edit</a>
</h3>
<p>To upgrade to 7.0, you’ll need to convert any joda-time date formats
to their java-time equivalents.</p>
<p>To help track this effort, you can prefix java-time date formats with an <code class="literal">8</code>
 in Elasticsearch 6.8 and later versions.</p>
<p>For example, you can change the date format <code class="literal">YYYY-MM-dd</code> to <code class="literal">8yyyy-MM-dd</code> to
indicate the date format uses java time.</p>
<p>Elasticsearch treats date formats starting with the <code class="literal">8</code> prefix differently depending on
the version:</p>
<p><span class="strong strong"><strong>6.8</strong></span>: Date formats with an <code class="literal">8</code> prefix are handled as java-time formats. Date
formats without an <code class="literal">8</code> prefix are treated as joda-time formats. We recommend
converting these joda-time formats to java-time <em>before</em> upgrading to 7.x.</p>
<p><span class="strong strong"><strong>7.x and later</strong></span>: For indices created in 6.x, date formats without an <code class="literal">8</code> prefix
are treated as joda-time formats. For indices created in 7.x and later versions,
all date formats are treated as java-time formats, regardless of whether it
starts with an <code class="literal">8</code> prefix.</p>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="java-time-migration-impacted-features"></a>Impacted features<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/migration/migrate_7_0/migrate_to_java_time.asciidoc">edit</a>
</h3>
</div></div></div>
<p>The switch to java time only impacts custom <a class="xref" href="date.html" title="Date datatype"><code class="literal">date</code></a> and
<a class="xref" href="date_nanos.html" title="Date nanoseconds datatype"><code class="literal">date_nanos</code></a> formats.</p>
<p>These formats are commonly used in:</p>
<div class="ulist itemizedlist">
<ul class="itemizedlist">
<li class="listitem">
<a class="xref" href="mapping.html" title="Mapping">Index mappings</a>
</li>
<li class="listitem">
<a class="xref" href="indices-templates.html" title="Put index template API">Index templates</a>
</li>
<li class="listitem">
<a class="xref" href="pipeline.html" title="Pipeline Definition">Ingest pipelines</a>
</li>
</ul>
</div>
<p>If you don’t use custom date formats, you can skip the rest of this guide.
Most custom date formats are compatible. However, several require
an update.</p>
<p>To see if your date format is impacted, use the <a class="xref" href="migration-api-deprecation.html" title="Deprecation info APIs">deprecation info API</a>
or the <a href="https://www.elastic.co/guide/en/kibana/7.7/upgrade-assistant.html" class="ulink" target="_top">Kibana upgrade assistant</a>.</p>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="java-time-migration-incompatible-date-formats"></a>Incompatible date formats<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/migration/migrate_7_0/migrate_to_java_time.asciidoc">edit</a>
</h3>
</div></div></div>
<p>Custom date formats containing the following joda-time literals should be
changed before upgrading.</p>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
<code class="literal">Y</code> (Year of era)
</span>
</dt>
<dd>
<p>Replace with <code class="literal">y</code>.</p>
<p><span class="strong strong"><strong>Example:</strong></span>
<code class="literal">YYYY-MM-dd</code> should become <code class="literal">yyyy-MM-dd</code>.</p>
<p>In java time, <code class="literal">Y</code> is used for
<a href="https://docs.oracle.com/javase/8/docs/api/java/time/temporal/WeekFields.html" class="ulink" target="_top">week-based year</a>.
Using <code class="literal">Y</code> in place of <code class="literal">y</code> could result in off-by-one errors in year calculation.</p>
<p>For pattern <code class="literal">YYYY-ww</code> and date <code class="literal">2019-01-01T00:00:00.000Z</code>  will give <code class="literal">2019-01</code>
For pattern <code class="literal">YYYY-ww</code> and date <code class="literal">2018-12-31T00:00:00.000Z</code>  will give <code class="literal">2019-01</code> (counter-intuitive) because there is &gt;4 days of that week in 2019</p>
</dd>
<dt>
<span class="term">
<code class="literal">y</code> (Year)
</span>
</dt>
<dd>
<p>Replace with <code class="literal">u</code>.</p>
<p><span class="strong strong"><strong>Example:</strong></span>
<code class="literal">yyyy-MM-dd</code> should become <code class="literal">uuuu-MM-dd</code>.</p>
<p>In java time, <code class="literal">y</code> is used for year of era. <code class="literal">u</code> can contain non-positive
values while <code class="literal">y</code> cannot. <code class="literal">y</code> can also be associated with an era field.</p>
</dd>
<dt>
<span class="term">
<code class="literal">C</code> (Century of era)
</span>
</dt>
<dd>
<p>Century of era is not supported in java time.
There is no replacement. Instead, we recommend you preprocess your input.</p>
</dd>
<dt>
<span class="term">
<code class="literal">x</code> (Week year)
</span>
</dt>
<dd>
<p>Replace with <code class="literal">Y</code>.</p>
<p>In java time, <code class="literal">x</code> means <a href="https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html" class="ulink" target="_top">zone-offset</a>.</p>
<div class="warning admon">
<div class="icon"></div>
<div class="admon_content">
<p>Failure to properly convert <code class="literal">x</code> (Week year) to <code class="literal">Y</code> could result in data loss.</p>
</div>
</div>
</dd>
<dt>
<span class="term">
<code class="literal">Z</code> (Zone offset/id)
</span>
</dt>
<dd>
<p>Replace with multiple <code class="literal">X</code>'s.</p>
<p><code class="literal">Z</code> has a similar meaning in java time. However, java time expects different
numbers of literals to parse different forms.</p>
<p>Consider migrating to <code class="literal">X</code>, which gives you more control over how time is parsed.
For example, the joda-time format <code class="literal">YYYY-MM-dd'T'hh:mm:ssZZ</code> accepts the following dates:</p>
<pre class="screen">2010-01-01T01:02:03Z
2010-01-01T01:02:03+01
2010-01-01T01:02:03+01:02
2010-01-01T01:02:03+01:02:03</pre>
<p>In java time, you cannot parse all these dates using a single format
Instead, you must specify 3 separate formats:</p>
<pre class="screen">2010-01-01T01:02:03Z
2010-01-01T01:02:03+01
both parsed with yyyy-MM-dd'T'hh:mm:ssX

2010-01-01T01:02:03+01:02
yyyy-MM-dd'T'hh:mm:ssXXX

2010-01-01T01:02:03+01:02:03
yyyy-MM-dd'T'hh:mm:ssXXXXX</pre>
<p>The formats must then be delimited using <code class="literal">||</code>:</p>
<div class="pre_wrapper lang-txt">
<pre class="programlisting prettyprint lang-txt">yyyy-MM-dd'T'hh:mm:ssX||yyyy-MM-dd'T'hh:mm:ssXXX||yyyy-MM-dd'T'hh:mm:ssXXXXX</pre>
</div>
<p>The same applies if you expect your pattern to occur without a colon (<code class="literal">:</code>):
For example, the <code class="literal">YYYY-MM-dd'T'hh:mm:ssZ</code> format accepts the following date forms:</p>
<pre class="screen">2010-01-01T01:02:03Z
2010-01-01T01:02:03+01
2010-01-01T01:02:03+0102
2010-01-01T01:02:03+010203</pre>
<p>To accept all these forms in java time, you must use the <code class="literal">||</code> delimiter:</p>
<div class="pre_wrapper lang-txt">
<pre class="programlisting prettyprint lang-txt">yyyy-MM-dd'T'hh:mm:ssX||yyyy-MM-dd'T'hh:mm:ssXX||yyyy-MM-dd'T'hh:mm:ssXXXX</pre>
</div>
</dd>
<dt>
<span class="term">
<code class="literal">d</code> (Day)
</span>
</dt>
<dd>
<p>In java time, <code class="literal">d</code> is still interpreted as "day" but is less flexible.</p>
<p>For example, the joda-time date format <code class="literal">YYYY-MM-dd</code> accepts <code class="literal">2010-01-01</code> or
<code class="literal">2010-01-1</code>.</p>
<p>In java time, you must use the <code class="literal">||</code> delimiter to provide specify each format:</p>
<div class="pre_wrapper lang-txt">
<pre class="programlisting prettyprint lang-txt">yyyy-MM-dd||yyyy-MM-d</pre>
</div>
<p>In java time, <code class="literal">d</code> also does not accept more than 2 digits. To accept days with more
than two digits, you must include a text literal in your java-time date format.
For example, to parse <code class="literal">2010-01-00001</code>, you must use the following java-time date format:</p>
<div class="pre_wrapper lang-txt">
<pre class="programlisting prettyprint lang-txt">yyyy-MM-'000'dd</pre>
</div>
</dd>
<dt>
<span class="term">
<code class="literal">e</code> (Name of day)
</span>
</dt>
<dd>
<p>In java time, <code class="literal">e</code> is still interpreted as "name of day" but does not parse
short- or full-text forms.</p>
<p>For example, the joda-time date format <code class="literal">EEE YYYY-MM</code> accepts both
<code class="literal">Wed 2020-01</code> and <code class="literal">Wednesday 2020-01</code>.</p>
<p>To accept both of these dates in java time, you must specify each format using
the <code class="literal">||</code> delimiter:</p>
<div class="pre_wrapper lang-txt">
<pre class="programlisting prettyprint lang-txt">cccc yyyy-MM||ccc yyyy-MM</pre>
</div>
<p>The joda-time literal <code class="literal">E</code> is interpreted as "day of week."
The java-time literal <code class="literal">c</code> is interpreted as "localized day of week."
<code class="literal">E</code> does not accept full-text day formats, such as <code class="literal">Wednesday</code>.</p>
</dd>
<dt>
<span class="term">
<code class="literal">EEEE</code> and similar text forms
</span>
</dt>
<dd>
<p>Support for full-text forms depends on the locale data provided with your Java
Development Kit (JDK) and other implementation details. We recommend you
test formats containing these patterns carefully before upgrading.</p>
</dd>
<dt>
<span class="term">
<code class="literal">z</code> (Time zone text)
</span>
</dt>
<dd>
<p>In java time, <code class="literal">z</code> outputs <em>Z</em> for Zulu when given a UTC timezone.</p>
</dd>
</dl>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="java-time-migration-test"></a>Test with your data<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/migration/migrate_7_0/migrate_to_java_time.asciidoc">edit</a>
</h4>
</div></div></div>
<p>We strongly recommend you test any date format changes using real data before
deploying in production.</p>
<p>For help with date debugging, consider using
<a href="https://esddd.herokuapp.com/" class="ulink" target="_top">https://esddd.herokuapp.com/.</a></p>
</div>

</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="java-time-migrate-update-mappings"></a>Update index mappings<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/migration/migrate_7_0/migrate_to_java_time.asciidoc">edit</a>
</h3>
</div></div></div>
<p>To update joda-time date formats in index mappings, you must create a new index
with an updated mapping and reindex your data to it.
You can however update your pipelines or templates.</p>
<p>The following <code class="literal">my_index_1</code> index contains a mapping for the <code class="literal">datetime</code> field, a
<code class="literal">date</code> field with a custom joda-time date format.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET my_index_1/_mapping</pre>
</div>
<div class="console_widget" data-snippet="snippets/2189.console"></div>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
  "my_index_1" : {
    "mappings" : {
      "properties" : {
         "datetime": {
           "type": "date",
           "format": "yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis"
         }
      }
    }
  }
}</pre>
</div>
<p>To change the date format for the <code class="literal">datetime</code> field, create a separate index
containing an updated mapping and date format.</p>
<p>For example, the following <code class="literal">my_index_2</code> index changes the <code class="literal">datetime</code> field’s
date format to <code class="literal">8uuuu/MM/dd HH:mm:ss||uuuu/MM/dd||epoch_millis</code>. The <code class="literal">8</code> prefix
indicates this date format uses java time.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT my_index_2
{
  "mappings": {
    "properties": {
      "datetime": {
        "type": "date",
        "format": "8uuuu/MM/dd HH:mm:ss||uuuu/MM/dd||epoch_millis"
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/2190.console"></div>
<p>Next, reindex data from the old index to the new index.</p>
<p>The following <a class="xref" href="docs-reindex.html" title="Reindex API">reindex</a> API request reindexes data from
<code class="literal">my_index_1</code> to <code class="literal">my_index_2</code>.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST _reindex
{
  "source": {
    "index": "my_index_1"
  },
  "dest": {
    "index": "my_index_2"
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/2191.console"></div>
<p>If you use index aliases, update them to point to the new index.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /_aliases
{
    "actions" : [
        { "remove" : { "index" : "my_index_1", "alias" : "my_index" } },
        { "add" : { "index" : "my_index_2", "alias" : "my_index" } }
    ]
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/2192.console"></div>
<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="java-time-migration-update-ingest-pipelines"></a>Update ingest pipelines<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/migration/migrate_7_0/migrate_to_java_time.asciidoc">edit</a>
</h4>
</div></div></div>
<p>If your ingest pipelines contain joda-time date formats, you can update them
using the <a class="xref" href="put-pipeline-api.html" title="Put pipeline API">put ingest pipeline</a> API.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT _ingest/pipeline/my_pipeline
{
  "description": "Pipeline for routing data to specific index",
  "processors": [
    {
      "date": {
        "field": "createdTime",
        "formats": [
         "8uuuu-w"
        ]
      },
      "date_index_name": {
        "field": "@timestamp",
        "date_rounding": "d",
        "index_name_prefix": "x-",
        "index_name_format": "8uuuu-w"
      }
    }
  ]
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/2193.console"></div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="java-time-migration-update-index-templates"></a>Update index templates<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/migration/migrate_7_0/migrate_to_java_time.asciidoc">edit</a>
</h4>
</div></div></div>
<p>If your index templates contain joda-time date formats, you can update them
using the <a class="xref" href="indices-templates.html" title="Put index template API">put index template</a> API.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT _template/template_1
{
  "index_patterns": [
    "te*",
    "bar*"
  ],
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "_source": {
      "enabled": false
    },
    "properties": {
      "host_name": {
        "type": "keyword"
      },
      "created_at": {
        "type": "date",
        "format": "8EEE MMM dd HH:mm:ss Z yyyy"
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/2194.console"></div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="java-time-migration-update-external-tools-templates"></a>Update external tools and templates<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/migration/migrate_7_0/migrate_to_java_time.asciidoc">edit</a>
</h4>
</div></div></div>
<p>Ensure you also update any date formats in templates or tools outside of Elasticsearch.
This can include tools such as <a href="https://www.elastic.co/guide/en/beats/libbeat/7.7/getting-started.html" class="ulink" target="_top">Beats</a> or
<a href="https://www.elastic.co/guide/en/logstash/7.7/index.html" class="ulink" target="_top">Logstash</a>.</p>
</div>

</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="breaking-changes-7.0.html">« Breaking changes in 7.0</a>
</span>
<span class="next">
<a href="es-release-notes.html">Release notes »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>